﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class eduExamquestion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            SetData();
    }


    protected void UltraWebGrid1_UpdateRow(object sender, Infragistics.WebUI.UltraWebGrid.RowEventArgs e)
    {
        SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["smsConnectionString"].ConnectionString);

        SqlDataAdapter da = new SqlDataAdapter("select * from QuestionInfo", objConnection);
        SqlCommandBuilder cb = new SqlCommandBuilder(da);

        DataSet ds = new DataSet();
        da.Fill(ds, "QuestionInfo");
        DataTable dt = ds.Tables["QuestionInfo"];

        if (e.Row.Cells[0].Value != null)
        {
            DataRow[] rows = dt.Select("QuestionID = " + (int)e.Row.Cells[0].Value + "");
            foreach (DataRow row in rows)
            {

                row["QuestionDes"] = e.Row.Cells[1];               
                row["Answer1"] = e.Row.Cells[2].Value;
                row["Answer2"] = e.Row.Cells[3].Value;
                row["Answer3"] = e.Row.Cells[4].Value;
                row["Answer4"] = e.Row.Cells[5].Value;
                row["RightAnswer"] = e.Row.Cells[6].Value;
                row["IsDeleted"] = false;
            }
        }
        else
        {
            DataRow row = dt.NewRow();
            row["QuestionDes"] = e.Row.Cells[1];
            row["CourDocumentID"] = Convert.ToInt32(Request.QueryString["FileID"]);            
            row["Answer1"] = e.Row.Cells[2].Value;
            row["Answer2"] = e.Row.Cells[3].Value;
            row["Answer3"] = e.Row.Cells[4].Value;
            row["Answer4"] = e.Row.Cells[5].Value;
            row["RightAnswer"] = e.Row.Cells[6].Value;
            row["IsDeleted"] = false;
            dt.Rows.Add(row);
        }

        da.Update(dt);
        SetData();
    }

    //添加新的题目
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/eduquestion.aspx?FileID=" + Request.QueryString["FileID"] + "");
    }

    //删除题目
    protected void Button2_Click1(object sender, EventArgs e)
    {
        SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["smsConnectionString"].ConnectionString);

        SqlDataAdapter da = new SqlDataAdapter("select * from QuestionInfo where IsDeleted<>1", objConnection);
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        DataSet ds = new DataSet();
        da.Fill(ds, "QuestionInfo");
        DataTable dt = ds.Tables["QuestionInfo"];
        DataRow[] rows = dt.Select("QuestionID = " + Convert.ToInt32(this.UltraWebGrid1.DisplayLayout.ActiveRow.DataKey) + "");
        foreach (DataRow row in rows)
            row["IsDeleted"] = true;
        da.Update(dt);
        SetData();
    }

    public void SetData()
    {
        ConnectionStringSettings connObj = ConfigurationManager.ConnectionStrings["smsConnectionString"];
        string connectionString = connObj.ConnectionString;
        SqlConnection objConnection = new SqlConnection(connectionString);

        SqlDataAdapter da = new SqlDataAdapter("SELECT QuestionID, QuestionDes AS 题目内容, RightAnswer AS 题目答案, Answer1 AS 选择A, Answer2 AS 选择B, Answer3 AS 选择C, Answer4 AS 选择D FROM QuestionInfo WHERE IsDeleted<>1 and CourDocumentID = " + Convert.ToInt32(Request.QueryString["FileID"]) + "", objConnection);

        DataSet ds = new DataSet();
        da.Fill(ds, "QuestionInfo");
        DataTable dt = ds.Tables["QuestionInfo"];
        DataView view = new DataView(dt);

        this.UltraWebGrid1.DataSource = view;
        this.UltraWebGrid1.DataKeyField = "QuestionID";
        this.UltraWebGrid1.DataBind();
        this.UltraWebGrid1.Columns[1].Hidden = true;

    }
}